<?php

if(isset($_POST)) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $name = $_POST['name'];
    $forename = $_POST['forename'];
    $email = $_POST['email'];
    $xmlFile = file_get_contents("./xml/users.xml");
    $xml = new SimpleXMLElement($xmlFile);
    //Ueberpruefen ob Users bereits existiert
    if($xml) {
        //Hoechste User_ID holen
        $new_id = count($xml->user)+1;
        $exists = false;
        foreach($xml->user as $user) {
            if($user->username == $username) {
                $exists = true;
            }
        }
        //Neuen Benutzer anlegen
        if(!$exists) {
            $user = $xml->addChild('user');
            $user->addAttribute('user_id', $new_id);
            $user->addAttribute('deleted', 'false');
            $user->addChild('username', $username);
            $user->addChild('name', $name);
            $user->addChild('forename', $forename);
            $user->addChild('email', $email);
        }
        
        if(!$exists) {
            $xml->asXML("./xml/users_neu.xml"); //Neues XML-Dokument speichern
        }
    }
}
?>

<html>
    <head>
        <title>Nebenkostenverwaltung</title>
    </head>
    <body>
        <form action="register.php" method="POST">
            <p>Benutzername:<br><input name="username" type="text" size="30" maxlength="30"></p>
            <p>Passwort:<br><input name="password" type="password" size="30" maxlength="40"></p>
            <p>Vorname:<br><input name="forename" type="text" size="30" maxlength="30"></p>
            <p>Nachname:<br><input name="name" type="text" size="30" maxlength="40"></p>
            <p>E-Mail:<br><input name="email" type="text" size="30" maxlength="40"></p>
            <input type="submit" value=" Absenden ">
            <input type="reset" value=" Abbrechen ">
        </form>
    </body>
</html>


